<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="hackathon1212.UserDAO" %>
<%
if(UserDAO.get().allowed(request.getUserPrincipal())){
%>
<!DOCTYPE html>
<html>
<head>
  <TITLE>MR API Test</TITLE>
  <style>img{ height: 100px; float: left; }</style>
  <script src="jquery-latest.js"></script>
</head>
<body>
  
<script>

function callselected()
{
	if($("#apifn").val() =="redeemcash")
	{
		hideres();
		redeemcash();
	}

	if($("#apifn").val() =="redeempoints")
	{
		hideres();
		redeempoints();

	}
	
	if($("#apifn").val() =="getpointbalance")
	{
		hideres();
		getbalance();

	}

	if($("#apifn").val() =="convertcashtopoints")
	{
		hideres();
		cashtopoints();
	}

	if($("#apifn").val() =="convertpointstocash")
	{
		hideres();
		pointstocash();

	}	

	if($("#apifn").val() =="searchcatalog")
	{
		hideres();
		searchcatalog();
	}	

	if($("#apifn").val() =="placeorder")
	{
		hideres();
		placeorder();
	}

	if($("#apifn").val() =="getbonusoffers")
	{
		hideres();
		getbonusoffers();
	}	

	if($("#apifn").val() =="enrolloffer")
	{
		hideres();
		enrolloffer();
	}		

}

function hideres()
{
		$("#balance1").hide();
		$("#cash1").hide();
		$("#points1").hide();
		$("#status1").hide();	
		$("#results").hide();				
	
}

function hidein()
{
		$("#inpoints1").hide();
		$("#incash1").hide();
		$("#pan1").hide();
		$("#item1").hide();
		$("#search1").hide();
		$("#bonus1").hide();
}


function selectchange()
{

	if($("#apifn").val() =="redeemcash")
	{
		$("#inpoints1").hide();
		$("#incash1").show();
		$("#pan1").show();
		$("#item1").hide();
		$("#search1").hide();
		$("#bonus1").hide();

		hideres();
	}

	if($("#apifn").val() =="redeempoints")
	{
		$("#inpoints1").show();
		$("#incash1").hide();
		$("#pan1").show();
		$("#item1").hide();
		$("#search1").hide();
		$("#bonus1").hide();

		hideres();
	}
	
	if($("#apifn").val() =="getpointbalance")
	{
		$("#inpoints1").hide();
		$("#incash1").hide();
		$("#pan1").show();
		$("#item1").hide();
		$("#search1").hide();
		$("#bonus1").hide();

		hideres();
	}

	if($("#apifn").val() =="convertcashtopoints")
	{
		$("#inpoints1").hide();
		$("#incash1").show();
		$("#pan1").show();
		$("#item1").hide();
		$("#search1").hide();
		$("#bonus1").hide();

		hideres();
	}

	if($("#apifn").val() =="convertpointstocash")
	{
		$("#inpoints1").show();
		$("#incash1").hide();
		$("#pan1").show();
		$("#item1").hide();
		$("#search1").hide();
		$("#bonus1").hide();

		hideres();		
	}	

	if($("#apifn").val() =="searchcatalog")
	{
		hideres();

		$("#inpoints1").hide();
		$("#incash1").hide();
		$("#pan1").show();		
		$("#item1").hide();
		$("#search1").show();
		$("#bonus1").hide();
	}	

	if($("#apifn").val() =="placeorder")
	{
		hideres();

		$("#inpoints1").hide();
		$("#incash1").hide();
		$("#pan1").show();
		$("#item1").show();
		$("#search1").hide();
		$("#bonus1").hide();
	}	
	
	if($("#apifn").val() =="getbonusoffers")
	{
		hideres();

		$("#inpoints1").hide();
		$("#incash1").hide();
		$("#pan1").show();
		$("#item1").hide();
		$("#search1").hide();
		$("#bonus1").hide();
	}	

	if($("#apifn").val() =="enrolloffer")
	{
		hideres();

		$("#inpoints1").hide();
		$("#incash1").hide();
		$("#pan1").show();
		$("#item1").hide();
		$("#search1").hide();
		$("#bonus1").show();
	}		

}

function getbonusoffers()
{

	deleterows();
	$("#results").append("<TABLE id=t1 border=1><TR><TD>Bonus Id</TD><TD>Bonus Type</TD><TD>Eligible Merchant</TD><TD>Merchant</TD></TR></TABLE>");

	$.getJSON("http://"+$("#jsonhost").val()+"/loyaltyservice/bonus/getbonusoffers.json?pan="+$("#pan").val()+"&jsonp=?",
  	function(data) {
	
		$("#status").val(data.status);
		if(data.status!="Success") 	
		{
			$("#status1").show();
		} 
		else 
		{
			//Iterate through results and dynamically insert into table		
			$.each(data.bonusoffers, function(i,item)
			{
				$("#t1 tr:last").after("<TR><TD>"+item.bonusid+"</TD><TD>"+item.bonustype+"</TD><TD>"+item.eligiblemerchant+"</TD><TD>"+item.merchant+"</TD></TR>");
    			});

			$("#results").show();				
		}
  	});


}

function enrolloffer()
{
  	$.getJSON("http://"+$("#jsonhost").val()+"/loyaltyservice/bonus/enrolloffer.json?pan="+$("#pan").val()+"&bonusid="+$("#bonus").val()+"&jsonp=?",
  	function(data) {
		
		$("#status").val(data.status);
		$("#status1").show();

  	});	

}


function searchcatalog()
{
  deleterows();
  $("#results").append("<TABLE id=t1 border=1><TR><TD>Item Id</TD><TD>Name</TD><TD>Make</TD><TD>Description</TD><TD>Cost</TD></TR></TABLE>");

  $.getJSON("http://"+$("#jsonhost").val()+"/loyaltyservice/searchcatalog.json?pan="+$("#pan").val()+"&searchkey="+$("#search").val()+"&jsonp=?",
  function(data) {
	
	$("#status").val(data.status);
	if(data.status!="Success") 	
	{
		$("#status1").show();
	} 
	else 
	{
		//Iterate through results and dynamically insert into table		
		$.each(data.searchresults, function(i,item)
		{
			$("#t1 tr:last").after("<TR><TD>"+item.itemid+"</TD><TD>"+item.name+"</TD><TD>"+item.make+"</TD><TD>"+item.description+"</TD><TD>"+item.cost+"</TD></TR>");
    		});

		$("#results").show();				
	}
  });
}

function deleterows()
{
	$("#t1").each(function() {
  		$this = $(this)
  		$this.remove();	
	});

}

function placeorder()
{
  $.getJSON("http://"+$("#jsonhost").val()+"/loyaltyservice/placeorder.json?pan="+$("#pan").val()+"&itemid="+$("#item").val()+"&jsonp=?",
  function(data) {
	$("#balance").val(data.balance);
	$("#status").val(data.status);
	if(data.status!="Success") 	
	{
		$("#status1").show();
	} 
	else 
	{
		$("#balance1").show();
	}
  });

}


function getbalance()
{

$.getJSON("http://"+$("#jsonhost").val()+"/loyaltyservice/getpointbalance.json?pan="+$("#pan").val()+"&jsonp=?",
  function(data) {
	$("#balance").val(data.balance);
	$("#status").val(data.status);
	if(data.status!="Success") 	
	{
		$("#status1").show();
	} 
	else 
	{
		$("#balance1").show();
	}
  });
}

function cashtopoints()
{

$.getJSON("http://"+$("#jsonhost").val()+"/loyaltyservice/convert/cashtopoints.json?pan="+$("#pan").val()+"&cashval="+$("#incash").val()+"&jsonp=?",
  function(data) {
	$("#points").val(data.points);
	$("#status").val(data.status);	

	if(data.status!="Success") 	
	{
		$("#status1").show();
	} 
	else 
	{
		$("#points1").show();
	}

  });
}

function pointstocash()
{

$.getJSON("http://"+$("#jsonhost").val()+"/loyaltyservice/convert/pointstocash.json?pan="+$("#pan").val()+"&pointsval="+$("#inpoints").val()+"&jsonp=?",
  function(data) {
	$("#cash").val(data.cash);	
	$("#status").val(data.status);
	
	if(data.status!="Success") 	
	{
		$("#status1").show();
	} 
	else 
	{
		$("#cash1").show();
	}
	
  });
}

function redeemcash()
{

$.getJSON("http://"+$("#jsonhost").val()+"/loyaltyservice/redeem/cash.json?pan="+$("#pan").val()+"&cashval="+$("#incash").val()+"&jsonp=?",
  function(data) {
	$("#balance").val(data.balance);
	$("#status").val(data.status);
	
	if(data.status!="Success") 	
	{
		$("#status1").show();
	} 
	else 
	{
		$("#balance1").show();
	}
	
  });
}

function redeempoints()
{

$.getJSON("http://"+$("#jsonhost").val()+"/loyaltyservice/redeem/points.json?pan="+$("#pan").val()+"&pointsval="+$("#inpoints").val()+"&jsonp=?",
  function(data) {
	$("#balance").val(data.balance);
	$("#status").val(data.status);
	
	if(data.status!="Success") 	
	{
		$("#status1").show();
	} 
	else 
	{
		$("#balance1").show();
	}
	
  });
}

$(document).ready(function() {
	hidein();
	hideres();
	//$("#jsonhost").val("localhost:8080");
	$("#jsonhost").val(window.location.host);
	
});


</script>

<H1>Membership Rewards API Test Page</H1>
<p>
Please select an API to call from the list below and complete the fields that are then presented.  Click Call API to execute the service.<BR>
Results will be displayed below for successful calls, a status will be shown if there are any errors. 
<p>

<DIV ID=host1>API Host <Input id=jsonhost type=textbox"></DIV>
<DIV ID=pan1">PAN <Input id=pan name=pan type=textbox maxlength=15></DIV>
<DIV ID=inpoints1>PTS <Input id=inpoints name=inpoints type=textbox maxlength=15></DIV>
<DIV ID=incash1>CASH <Input id=incash name=incash type=textbox maxlength=15></DIV>
<DIV ID=item1>Item Id <Input id=item name=item type=textbox></DIV>
<DIV ID=bonus1>Bonus Id <Input id=bonus name=bonus type=textbox></DIV>
<DIV ID=search1>Search Key <Input id=search name=search type=textbox></DIV>
<P> 

<select name="apifn" id="apifn" title="" onchange=selectchange()>
<option value="">Select API To Call</option>
<option value="redeemcash">RedeemCash</option>
<option value="redeempoints">RedeemPoints</option>
<option value="getpointbalance">GetPointBalance</option>
<option value="convertcashtopoints">ConvertCashtoPoints</option>
<option value="convertpointstocash">ConvertPointstoCash</option>
<option value="searchcatalog">SearchCatalog</option>
<option value="placeorder">PlaceOrder</option>
<option value="getbonusoffers">GetBonusOffers</option>
<option value="enrolloffer">EnrollOffer</option>

</select> 

<INPUT TYPE=button value="Call API" onclick=callselected()>

<DIV ID=balance1>Balance <INPUT TYPE=textbox id="balance"></DIV>
<DIV ID=cash1>Cash <INPUT TYPE=textbox id="cash"></DIV>
<DIV ID=points1>Points <INPUT TYPE=textbox id="points"></DIV>
<DIV ID=status1>Status <INPUT TYPE=textbox id="status"></DIV>
<DIV ID=results><TABLE id=t1 border=1><TR><TD>Item Id</TD><TD>Name</TD><TD>Make</TD><TD>Description</TD><TD>Cost</TD></TR></TABLE></DIV>


</body>
</html>
<%}else{%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>No Content</title>
</head>
<body>
No Content
</body>
</html>
<%}%>